package com.example.online.music.controller;

import com.example.online.music.common.R;
import com.example.online.music.model.domain.ListSong;
import com.example.online.music.model.domain.Song;
import com.example.online.music.model.request.SongRequest;
import com.example.online.music.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SongController {

    @Autowired
    private SongService songService;


    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
        factory.setMaxFileSize(DataSize.of(20, DataUnit.MEGABYTES));
        // 设置总上传数据总大小10M
        factory.setMaxRequestSize(DataSize.of(20, DataUnit.MEGABYTES));
        return factory.createMultipartConfig();
    }

    /**
     * 后台下载歌曲
     * @param songId
     * @param userId
     * @param response
     * @throws Exception
     */
    @GetMapping("/song/localDownload")
    public void localDownload(@RequestParam("songId") Integer songId,
                              @RequestParam("userId") Integer userId,
                              HttpServletResponse response) throws Exception {
        songService.downloadSong(songId,userId,response);
    }

    /**
     * 判断歌曲是否已支付
     * @param songId
     * @param userId
     * @return
     */
    @GetMapping("/song/isPay")
    public R isPay(@RequestParam("songId") Integer songId,
                   @RequestParam("userId") Integer userId)  {
        return songService.isPay(songId,userId);
    }

    // 添加歌曲
    @PostMapping("/song/add")
    public R addSong(SongRequest addSongRequest, @RequestParam("file") MultipartFile mpfile) {
        return songService.addSong(addSongRequest,mpfile);
    }

    // 删除歌曲
    @DeleteMapping("/song/delete")
    public R deleteSong(@RequestParam int id) {
        return songService.deleteSong(id);
    }

    // 返回所有歌曲
    @GetMapping("/song")
    public R allSong() {
        return songService.allSong();
    }

    //TODO ok
    // 返回指定歌曲ID的歌曲
    @GetMapping("/song/detail")
    public R songOfId(@RequestParam int id) {
        return songService.songOfId(id);
    }

    // 返回指定歌手ID的歌曲
    @GetMapping("/song/singer/detail")
    public R songOfSingerId(@RequestParam int singerId) {
        /*List<Song> list = new ArrayList<>();
        Song s = new Song();
        s.setId(1);
        s.setSingerId(2);
        s.setName("周杰伦-稻香");
        s.setIntroduction("魔杰座");
        s.setPic("/img/songPic/daoxiang.jpg");
        s.setUrl("/song/周杰伦-稻香.mp3");
        list.add(s);
        return R.success("查询成功", list);*/
        return songService.songOfSingerId(singerId);
    }

    // 返回指定歌手名的歌曲
    @GetMapping("/song/singerName/detail")
    public R songOfSingerName(@RequestParam String name) {
        return songService.songOfSingerName('%' + name + '%');
    }

    // 更新歌曲信息
    @PostMapping("/song/update")
    public R updateSongMsg(@RequestBody SongRequest updateSongRequest) {
        return songService.updateSongMsg(updateSongRequest);
    }

    // 更新歌曲图片
    @PostMapping("/song/img/update")
    public R updateSongPic(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        return songService.updateSongPic(urlFile, id);
    }

    // 更新歌曲
    @PostMapping("/song/url/update")
    public R updateSongUrl(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        return songService.updateSongUrl(urlFile, id);
    }

    /**
     * @Description: 推荐歌曲列表（分页）
     * @Author: yang_yong
     * @CreateTime:
     * @return: com.example.online.music.common.R
     **/
    @GetMapping("/song/recommend/{userId}")
    public R recommend(@PathVariable(value = "userId", required = false) Integer userId) {
        /*List<Song> list = new ArrayList<>();
        Song s = new Song();
        s.setId(1);
        s.setSingerId(2);
        s.setName("周杰伦-稻香");
        s.setIntroduction("魔杰座");
        s.setPic("/img/songPic/daoxiang.jpg");
        s.setUrl("/song/周杰伦-稻香.mp3");
        list.add(s);
        //TODO 这里需要调用 Service 接口返回实际数据
        return R.success("查询成功", list);*/
        return songService.getRecommendSongList(userId);
    }
}
